package com.czc.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    /*
        自定义登录逻辑
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        System.out.println("执行自定义登录逻辑");

        //1.根据用户名去数据库查询,如果不存在就抛异常
        if(!"czc".equals(username)){
            throw new UsernameNotFoundException("用户名不存在");
        }

        //2.比较密码 用户注册存的肯定是密文  如果匹配成功返回UserDetails
        String encode = passwordEncoder.encode("123");

        return new User(username,encode, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
